Object.defineProperty(exports, "__esModule", {
  value: true
});
var n = [",", ";", "\t", "|", "^"];
var i = ["\r\n", "\r", "\n"];
var s = function s(e, t, a, n) {
  if (a instanceof Array) {
    if ("number" === a[t]) {
      return Number(n[t]);
    }
    if ("boolean" === a[t]) {
      return "true" === n[t] || "t" === n[t] || "1" === n[t];
    }
    if ("string[]" === a[t]) {
      return n[t].split(",");
    }
    if ("number[]" === a[t]) {
      if ("" == n[t]) {
        return [];
      } else {
        return n[t].replace(/\[/g, "").replace(/\]/g, "").split(",").map(Number);
      }
    }
    if ("number[][]" === a[t]) {
      var _e13 = n[t].replace(/\[/g, "").replace(/\]/g, "");
      _e13 = _e13.split(",").map(Number);
      return _e13 = o(_e13);
    }
    if ("string[][]" === a[t]) {
      var _e14 = n[t].replace(/\[/g, "").replace(/\]/g, "").split(",");
      return _e14 = o(_e14);
    }
    if ("number[][][]" === a[t]) {
      var _e15 = n[t].split("-");
      return _e15 = l(_e15, true);
    }
    return n[t];
  }
  if (isNaN(Number(e))) {
    if ("false" == e || "true" == e || "t" == e || "f" == e) {
      return "true" === n[t] || "t" === n[t] || "1" === n[t];
    } else {
      return n[t];
    }
  } else {
    return Number(n[t]);
  }
};
var o = function o(e) {
  var t = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  var a = e.length;
  var n = a % t == 0 ? a / t : Math.floor(a / t + 1);
  var i = [];
  for (var _s = 0; _s < n; _s++) {
    var _a11 = e.slice(_s * t, _s * t + t);
    i.push(_a11);
  }
  return i;
};
var l = function l(e) {
  var t = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var a = [];
  for (var _n21 = 0; _n21 < e.length; _n21++) {
    e[_n21] = e[_n21].replace(/\[/g, "").replace(/\]/g, "").split(",");
    t && (e[_n21] = e[_n21].map(Number));
    e[_n21] = o(e[_n21]);
    a.push(e[_n21]);
  }
  return a;
};
var def_CSVParser = {
  STANDARD_DECODE_OPTS: {
    skip: 0,
    limit: false,
    header: false,
    cast: false,
    comment: ""
  },
  STANDARD_ENCODE_OPTS: {
    delimiter: n[0],
    newline: i[0],
    skip: 0,
    limit: false,
    header: false
  },
  quoteMark: '"',
  doubleQuoteMark: '""',
  quoteRegex: /"/g,
  assign() {
    var e = Array.prototype.slice.call(arguments);
    var t = e[0];
    var a = e.slice(1);
    var n = 0;
    for (var i = a.length; n < i; n++) {
      for (var s in a[n]) {
        t[s] = a[n][s];
      }
    }
    return t;
  },
  map(e, t) {
    var a = [];
    var n = 0;
    for (var i = e.length; n < i; n++) {
      a[n] = t(e[n], n);
    }
    return a;
  },
  getType(e) {
    return Object.prototype.toString.call(e).slice(8, -1);
  },
  getLimit(e, t) {
    if (false === e) {
      return t;
    } else {
      return e;
    }
  },
  buildObjectConstructor(e, t, a) {
    return function (n) {
      var i = new Object();
      var o = function o(e, t) {
        return i[e] = t;
      };
      if (a) {
        e.forEach(function (e, i) {
          o(e, s(t[i], i, a, n));
        });
      } else {
        e.forEach(function (e, a) {
          o(e, s(t[a], a, null, n));
        });
      }
      return i;
    };
  },
  buildArrayConstructor(e, t, a) {
    return function (n) {
      var i = new Array(t.length);
      var o = function o(e, t) {
        return i[e] = t;
      };
      if (a) {
        e.forEach(function (e, i) {
          o(e, s(t[i], i, a, n));
        });
      } else {
        e.forEach(function (e, a) {
          o(e, s(t[a], a, null, n));
        });
      }
      return i;
    };
  },
  frequency(e, t, a) {
    undefined === a && (a = false);
    var n = 0;
    var i = 0;
    for (var s = this.getLimit(a, e.length); i < s && -1 !== (i = e.indexOf(t, i));) {
      i += 1;
      n++;
    }
    return n;
  },
  mostFrequent(e, t, a) {
    var n;
    for (var i = t.length - 1; i >= 0; i--) {
      this.frequency(e, t[i], a) > 0 && (n = t[i]);
    }
    return n || t[0];
  },
  unsafeParse(e, t, a) {
    var n;
    var i;
    var s = e.split(t.newline);
    function o(e) {
      var a = e.shift();
      if (a.indexOf('"') >= 0) {
        var n = 0;
        var i = 0;
        for (var s = 0; e.length > 0 && (-1 !== (n = a.indexOf('"', i)) || s % 2 != 0);) {
          if (-1 !== n) {
            i = n + 1;
            s++;
          } else {
            a = a + t.newline + e.shift();
          }
        }
        var o;
        var l = [];
        var r = 0;
        var c = 0;
        var d = 0;
        var h = a.length;
        for (var u in a) {
          if (!a.hasOwnProperty(u)) {
            continue;
          }
          var _e16 = parseInt(u);
          var g = a[u];
          if (0 === _e16 && '"' === g) {
            r++;
            c = 1;
          }
          if ('"' === g) {
            r++;
            a[_e16 - 1] === t.delimiter && c === _e16 && c++;
          }
          '"' === g && r % 2 == 0 && (a[_e16 + 1] !== t.delimiter && _e16 + 1 !== h || (d = _e16, o = a.substring(c, d), l.push(o), d = c = d + 2));
          if (g === t.delimiter && r % 2 == 0) {
            if ((d = _e16) > c) {
              o = a.substring(c, d);
              l.push(o);
              d = c = d + 1;
            } else if (d === c) {
              l.push(""), d = c = d + 1;
            }
          }
        }
        if ((d = h) >= c) {
          o = a.substring(c, d);
          l.push(o);
        }
        return l;
      }
      return a.split(t.delimiter);
    }
    t.skip > 0 && s.splice(t.skip);
    for (t.header ? (true === t.header ? (t.comment = o(s), t.cast = o(s), n = o(s)) : "Array" === this.getType(t.header) && (n = t.header), i = this.buildObjectConstructor(n, s[0].split(t.delimiter), t.cast)) : i = this.buildArrayConstructor(n, s[0].split(t.delimiter), t.cast); s.length > 0;) {
      var l = o(s);
      l.length > 1 && a(i(l), n[0]);
    }
    return true;
  },
  safeParse(e, t) {
    t.delimiter;
    var a = t.newline;
    var n = e.split(a);
    t.skip > 0 && n.splice(t.skip);
    return true;
  },
  encodeCells(e, t, a) {
    var n = e.slice(0);
    var i = 0;
    for (var s = n.length; i < s; i++) {
      -1 !== n[i].indexOf(this.quoteMark) && (n[i] = n[i].replace(this.quoteRegex, this.doubleQuoteMark));
      -1 === n[i].indexOf(t) && -1 === n[i].indexOf(a) || (n[i] = this.quoteMark + n[i] + this.quoteMark);
    }
    return n.join(t);
  },
  encodeArrays(e, t, a) {
    var n = t.delimiter;
    var i = t.newline;
    t.header && "Array" === this.getType(t.header) && a(this.encodeCells(t.header, n, i));
    var s = 0;
    for (var o = this.getLimit(t.limit, e.length); s < o; s++) {
      a(this.encodeCells(e[s], n, i));
    }
    return true;
  },
  encodeObjects(e, t, a) {
    var n;
    var i;
    var s = t.delimiter;
    var o = t.newline;
    n = [];
    i = [];
    for (var l in e[0]) {
      n.push(l);
      i.push(e[0][l]);
    }
    if (true === t.header) {
      a(this.encodeCells(n, s, o));
    } else {
      "Array" === this.getType(t.header) && a(this.encodeCells(t.header, s, o));
    }
    a(this.encodeCells(i, s));
    var r = 1;
    for (var c = this.getLimit(t.limit, e.length); r < c; r++) {
      i = [];
      var d = 0;
      for (var h = n.length; d < h; d++) {
        i.push(e[r][n[d]]);
      }
      a(this.encodeCells(i, s, o));
    }
    return true;
  },
  parse(e, t, a) {
    var s;
    if ("Function" === this.getType(t)) {
      a = t;
      t = {};
    } else if ("Function" !== this.getType(a)) {
      a = (s = []).push.bind(s);
    } else {
      s = [];
    }
    t = this.assign({}, this.STANDARD_DECODE_OPTS, t);
    this.opts = t;
    if (!t.delimiter || !t.newline) {
      var o = Math.min(48, Math.floor(e.length / 20), e.length);
      t.delimiter = t.delimiter || this.mostFrequent(e, n, o);
      t.newline = t.newline || this.mostFrequent(e, i, o);
    }
    return this.unsafeParse(e, t, a) && (!(s.length > 0) || s);
  },
  encode(e, t, a) {
    var n;
    if ("Function" === this.getType(t)) {
      a = t;
      t = {};
    } else {
      "Function" !== this.getType(a) && (a = (n = []).push.bind(n));
    }
    (t = this.assign({}, this.STANDARD_ENCODE_OPTS, t)).skip > 0 && (e = e.slice(t.skip));
    return ("Array" === this.getType(e[0]) ? this.encodeArrays : this.encodeObjects)(e, t, a) && (!(n.length > 0) || n.join(t.newline));
  }
};
exports.default = def_CSVParser;